<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>小说作品提交表单</title>
  <style>
    :root {
      --primary-color: #4a6cf7;
      --secondary-color: #f0f2ff;
      --text-color: #333;
      --light-text: #666;
      --border-color: #e0e4f0;
      --success-color: #2ecc71;
      --error-color: #e74c3c;
      --background-color: #f9fafc;
      --container-bg: #ffffff;
    }

    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
      font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    }

    body {
      background-color: var(--background-color);
      color: var(--text-color);
      line-height: 1.6;
      padding: 20px;
    }

    .container {
      max-width: 800px;
      margin: 0 auto;
      background-color: var(--container-bg);
      border-radius: 12px;
      box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
      overflow: hidden;
    }

    .form-header {
      background-color: var(--primary-color);
      color: white;
      padding: 30px 25px;
      text-align: center;
    }

    .form-header h1 {
      font-size: 28px;
      margin-bottom: 10px;
      font-weight: 600;
    }

    .form-header p {
      font-size: 16px;
      opacity: 0.9;
    }

    .form-body {
      padding: 30px 25px;
    }

    .form-group {
      margin-bottom: 25px;
    }

    .form-group label {
      display: block;
      margin-bottom: 8px;
      font-weight: 500;
      color: var(--text-color);
    }

    .form-group small {
      display: block;
      margin-top: 5px;
      font-size: 13px;
      color: var(--light-text);
    }

    .form-control {
      width: 100%;
      padding: 12px 15px;
      border: 1px solid var(--border-color);
      border-radius: 8px;
      font-size: 16px;
      transition: border-color 0.3s;
    }

    .form-control:focus {
      outline: none;
      border-color: var(--primary-color);
      box-shadow: 0 0 0 3px rgba(74, 108, 247, 0.2);
    }

    .form-control-textarea {
      min-height: 120px;
      resize: vertical;
    }

    .form-control-file {
      padding: 10px 0;
    }

    .form-control-file input {
      display: none;
    }

    .file-upload-btn {
      display: inline-block;
      padding: 10px 20px;
      background-color: var(--secondary-color);
      color: var(--primary-color);
      border: none;
      border-radius: 6px;
      cursor: pointer;
      font-weight: 500;
      transition: background-color 0.3s;
    }

    .file-upload-btn:hover {
      background-color: #e6e9ff;
    }

    .file-preview {
      margin-top: 10px;
      padding: 15px;
      border: 1px dashed var(--border-color);
      border-radius: 8px;
      background-color: var(--secondary-color);
      display: none;
      text-align: center;
    }

    .file-preview.active {
      display: block;
    }

    .file-preview img {
      max-width: 200px;
      max-height: 300px;
      border-radius: 6px;
      margin-bottom: 10px;
      display: block;
      margin-left: auto;
      margin-right: auto;
    }

    .file-preview .file-info {
      margin-top: 10px;
      font-size: 14px;
      color: var(--text-color);
    }

    .radio-group,
    .checkbox-group {
      display: flex;
      flex-wrap: wrap;
      gap: 15px;
      margin-top: 10px;
    }

    .radio-option,
    .checkbox-option {
      display: flex;
      align-items: center;
      gap: 8px;
    }

    .form-error {
      color: var(--error-color);
      font-size: 13px;
      margin-top: 5px;
      display: none;
    }

    .form-error.active {
      display: block;
    }

    .form-success {
      color: var(--success-color);
      font-size: 13px;
      margin-top: 5px;
      display: none;
    }

    .form-success.active {
      display: block;
    }

    .form-footer {
      padding: 20px 25px;
      background-color: var(--secondary-color);
      text-align: center;
    }

    .submit-btn {
      padding: 12px 30px;
      background-color: var(--primary-color);
      color: white;
      border: none;
      border-radius: 8px;
      font-size: 16px;
      font-weight: 500;
      cursor: pointer;
      transition: background-color 0.3s;
      display: inline-flex;
      align-items: center;
      gap: 8px;
    }

    .submit-btn:hover {
      background-color: #3a5ce6;
    }

    .submit-btn:disabled {
      background-color: #90a3ff;
      cursor: not-allowed;
    }

    .form-section-title {
      font-size: 20px;
      margin-bottom: 20px;
      padding-bottom: 10px;
      border-bottom: 2px solid var(--border-color);
      color: var(--primary-color);
    }

    .required-field::after {
      content: " *";
      color: var(--error-color);
    }

    @media (max-width: 600px) {

      .form-body,
      .form-header,
      .form-footer {
        padding: 20px 15px;
      }

      .form-header h1 {
        font-size: 24px;
      }

      .file-preview img {
        max-width: 100%;
        max-height: 200px;
      }
    }
  </style>
  <script src="js/jquery.3.7.1min.js"></script>
</head>

<body>
  <div class="container">
    <div class="form-header">
      <h1>新书注册</h1>
      <p>欢迎将您的作品提交给我们，我们期待阅读您的精彩创作！</p>
    </div>

    <div class="form-body">
      <form id="novel-submission-form" action="/qimao/writer/submitNovel.do" method="post"
        enctype="multipart/form-data">
        <h2 class="form-section-title">小说信息</h2>
        <input type="hidden" name="authorId" value="">
        <div class="form-group">
          <label for="novel-title" class="required-field">书名</label>
          <input type="text" id="novel-title" name="novelTitle" class="form-control" placeholder="请输入书名">
          <div class="form-error" id="novel-title-error">请输入书名</div>
          <div class="form-success" id="novel-title-success">格式正确</div>
        </div>

        <!-- 单一类型选择框 - 添加了name属性 -->
        <div class="form-group">
          <label for="novel-category" class="required-field">小说类型</label>
          <select id="novel-category" name="novelCategory" class="form-control">
            <option value="">请选择小说类型</option>
            <option value="1">东方玄幻</option>
            <option value="2">都市言情</option>
            <option value="3">科幻未来</option>
            <option value="4">悬疑推理</option>
          </select>
          <div class="form-error" id="novel-category-error">请选择小说类型</div>
        </div>

        <!-- 作品简介（不少于20字） -->
        <div class="form-group">
          <label for="novel-introduction" class="required-field">作品简介</label>
          <textarea id="novel-introduction" name="novelIntroduction" class="form-control form-control-textarea"
            placeholder="请简要介绍您的作品（不少于20字）"></textarea>
          <div class="form-error" id="introduction-error">请填写作品简介，不少于20字</div>
          <div class="form-success" id="introduction-success">简介格式正确</div>
        </div>

        <!-- 是否原创功能 -->
        <div class="form-group">
          <label for="is-original" class="required-field">是否原创</label>
          <div class="radio-group" id="original-radio-group">
            <div class="radio-option">
              <input type="radio" id="original-yes" name="isOriginal" value="yes">
              <label for="original-yes">是，本人原创作品</label>
            </div>
            <div class="radio-option">
              <input type="radio" id="original-no" name="isOriginal" value="no">
              <label for="original-no">否，改编/翻译/其他</label>
            </div>
          </div>
          <div class="form-error" id="original-error">请选择作品是否为原创</div>
        </div>

        <!-- 小说封面上传 - 非必填 -->
        <div class="form-group">
          <label for="cover-image">小说封面（可选）</label>
          <p class="form-control-file">
            <label for="cover-upload" class="file-upload-btn">
              选择封面图片
            </label>
            <input type="file" id="cover-upload" name="coverImage" accept="image/jpeg,image/png" class="form-control" />
          </p>
          <div class="file-preview" id="cover-preview">
            <img src="" alt="封面预览" id="cover-preview-img">
            <div class="file-info" id="cover-file-info"></div>
            <button type="button" id="remove-cover" class="file-upload-btn" style="margin-top: 10px;">
              移除封面
            </button>
          </div>
          <div class="form-error" id="cover-error">封面图片需为JPG、PNG格式，不超过5MB</div>
          <div class="form-success" id="cover-success">封面上传成功</div>
        </div>
      </form>
    </div>

    <div class="form-footer">
      <button type="submit" form="novel-submission-form" class="submit-btn" id="submit-btn">
        <span>提交作品</span>
        <svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
          <path
            d="M12 22C17.5228 22 22 17.5228 22 12C22 6.47715 17.5228 2 12 2C6.47715 2 2 6.47715 2 12C2 17.5228 6.47715 22 12 22Z"
            stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" />
          <path d="M16 12L12 16L8 12" stroke="currentColor" stroke-width="2" stroke-linecap="round"
            stroke-linejoin="round" />
          <path d="M12 8V16" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" />
        </svg>
      </button>
    </div>
  </div>

  <script>
    document.addEventListener('DOMContentLoaded', function () {
      const form = document.getElementById('novel-submission-form');
      let authorId = null; // 存储作者ID

      // 原创相关元素
      const originalYes = document.getElementById('original-yes');
      const originalNo = document.getElementById('original-no');
      const originalError = document.getElementById('original-error');

      // 封面上传相关元素
      const coverUpload = document.getElementById('cover-upload');
      const coverPreview = document.getElementById('cover-preview');
      const coverPreviewImg = document.getElementById('cover-preview-img');
      const coverFileInfo = document.getElementById('cover-file-info');
      const removeCover = document.getElementById('remove-cover');
      const coverError = document.getElementById('cover-error');
      const coverSuccess = document.getElementById('cover-success');

      // 作品简介相关元素
      const introduction = document.getElementById('novel-introduction');
      const introductionError = document.getElementById('introduction-error');
      const introductionSuccess = document.getElementById('introduction-success');

      // 类型选择框
      const categorySelect = document.getElementById('novel-category');

      function selectCategory() {
        $.ajax({
          type: "GET",
          url: "getCategory.do",
          success: function (response) {
            console.log("获取类型数据成功", response);
            const categories = response.data || [];

            // 清空原有选项
            categorySelect.innerHTML = '<option value="">请选择小说类型</option>';

            // 使用$.each遍历数据并填充选项
            $.each(categories, function (index, category) {
              const option = document.createElement('option');
              option.value = category.minorId;
              option.textContent = category.minorName;
              categorySelect.appendChild(option);
            });
          },
          error: function (xhr, status, error) {
            console.error("获取类型数据失败", error);
            alert("获取小说类型数据失败，请稍后再试");
          }
        });
      }

      // 获取作者ID
      function fetchAuthorId() {
        $.ajax({
          type: "GET",
          url: "checkSession.do",
          success: function (data) {
            console.log("获取用户信息成功", data);
            if (data.data && data.data.authorId) {
              authorId = data.data.authorId;
              form.elements['authorId'].value = authorId;
            } else {
              alert("未获取到作者ID，请重新登录");
              window.location.href = "login.html"; // 重定向到登录页
            }
          },
          error: function (xhr, status, error) {
            console.error("获取用户信息失败", error);
            alert("获取用户信息失败，请稍后再试");
            window.location.href = "login.html"; // 重定向到登录页
          }
        });
      }

      fetchAuthorId(); // 页面加载时获取作者ID
      selectCategory(); // 页面加载时获取类型数据

      // 封面上传功能
      coverUpload.addEventListener('change', function (e) {
        const file = e.target.files[0];
        if (file) {
          validateCoverFile(file);
        }
      });

      // 移除封面
      removeCover.addEventListener('click', function () {
        coverUpload.value = '';
        coverPreview.classList.remove('active');
        coverPreviewImg.src = '';
        coverFileInfo.textContent = '';
        coverError.classList.remove('active');
        coverSuccess.classList.remove('active');
      });

      // 验证封面文件
      function validateCoverFile(file) {
        const fileSize = file.size / (1024 * 1024); // 转换为MB
        const fileType = file.type;

        // 验证文件类型和大小
        if (fileSize > 5) {
          showCoverError('封面图片大小不能超过5MB');
          return false;
        }

        if (!fileType.match('image/jpeg') && !fileType.match('image/png')) {
          showCoverError('请上传JPG或PNG格式的图片');
          return false;
        }

        // 显示预览
        showCoverPreview(file);
        return true;
      }

      // 显示封面预览
      function showCoverPreview(file) {
        const reader = new FileReader();

        reader.onload = function (e) {
          coverPreviewImg.src = e.target.result;
          coverFileInfo.textContent = `文件名: ${file.name} (${formatFileSize(file.size)})`;
          coverPreview.classList.add('active');
          coverError.classList.remove('active');
          coverSuccess.classList.add('active');
        }

        reader.readAsDataURL(file);
      }

      // 显示封面错误
      function showCoverError(message) {
        coverError.textContent = message;
        coverError.classList.add('active');
        coverSuccess.classList.remove('active');
        coverPreview.classList.remove('active');
      }

      // 格式化文件大小
      function formatFileSize(bytes) {
        if (bytes < 1024) return bytes + ' B';
        else if (bytes < 1048576) return (bytes / 1024).toFixed(2) + ' KB';
        else return (bytes / 1048576).toFixed(2) + ' MB';
      }

      // 作品简介验证
      introduction.addEventListener('input', function () {
        const introText = this.value.trim();
        if (introText.length >= 20) {
          introductionError.classList.remove('active');
          introductionSuccess.classList.add('active');
        } else if (introText.length > 0) {
          introductionError.textContent = '作品简介需不少于20字';
          introductionError.classList.add('active');
          introductionSuccess.classList.remove('active');
        } else {
          introductionError.textContent = '请填写作品简介';
          introductionError.classList.add('active');
          introductionSuccess.classList.remove('active');
        }
      });

    });
  </script>
</body>

</html>